package net.hn.hnms.biz.gasext.dws.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.hn.hnms.biz.api.safety.gasext.domain.CurveVo;
import net.hn.hnms.biz.api.safety.location.model.LeadVo;
import net.hn.hnms.biz.gasext.dws.domain.dto.EffectiveProductionTimeByMineCodeDto;
import net.hn.hnms.biz.gasext.dws.domain.dto.PeopleAndleadNumberVo;
import net.hn.hnms.biz.gasext.dws.domain.vo.*;
import net.hn.hnms.biz.gasext.dws.mapper.*;
import net.hn.hnms.biz.gasext.dws.service.AqjkService;
import net.hn.hnms.biz.gasext.dws.service.DwdService;
import net.hn.hnms.biz.gasext.dws.service.DwsService;
import net.hn.hnms.biz.gasext.dws.service.RydwService;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.utils.DateUtils;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.redis.domain.SysOrganization;
import net.hn.hnms.biz.gasext.dws.domain.*;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/dwsMonitor")
@Api(tags = {"dws接口"})
public class DwsController {
    @Resource
    private AqjkService aqjkService;

    @Resource
    private RydwService rydwService;

    @Resource
    private SysOrganizationDwsMapper sysOrganizationMapper;

    @Resource
    private RydwRyssMapper rydwRyssMapper;

    @Resource
    private DwsService dwsService;
    @Resource
    private RydwRyjzMapper rydwRyjzMapper;

    @Resource
    private JyglDwmcEntityMapper jyglDwmcEntityMapper;
    @Resource
    private JyglShiftManagementMapper jyglShiftManagementMapper;
    @Resource
    private JyglInsertShiftManage1Mapper jyglInsertShiftManage1Mapper;

//    @GetMapping("selectCodeSubList")
//    @ApiOperation("获取煤业公司煤矿树结构")
//    public R<SysOrganizationVo> selectCodeSubList() {
//        SysOrganizationVo sysOrganizationVo = aqjkService.selectCodeSubList();
//        return R.ok(sysOrganizationVo);
//    }

    @Resource
    private DwdService dwdService;
    @GetMapping("selectCodeSubMineList")
    @ApiOperation("获取煤业公司所有煤矿列表")
    public R<List<SysOrganization>> selectCodeSubMineList() {
        List<SysOrganization> list = aqjkService.selectCodeSubMineList();
        return R.ok(list);
    }
    @GetMapping("selectCodeUndergroundMiningSubMineList")
    @ApiOperation("获取煤业公司井工矿煤矿列表")
    public R<List<SysOrganization>> selectCodeUndergroundMiningSubMineList() {
        List<SysOrganization> list = aqjkService.selectCodeUndergroundMiningSubMineList();
        return R.ok(list);
    }
    @GetMapping("selectCodeOutdoorsSubMineList")
    @ApiOperation("获取煤业公司露天矿煤矿列表")
    public R<List<SysOrganization>> selectCodeOutdoorsSubMineList() {
        List<SysOrganization> list = aqjkService.selectCodeOutdoorsSubMineList();
        return R.ok(list);
    }
    @GetMapping("selectTrail")
    @ApiOperation("人员信息")
    public R<RydwRyssPo> selectTrail(@ApiParam(value = "人员卡号", required = true) String staffCode, @ApiParam(value = "入井时间（yyyy-MM-dd HH:mm:ss）"
            , required = true) String inTime
            , String mineCode) {
        RydwRyssPo rydwRyssPo = rydwService.selectTrail(staffCode, inTime, mineCode);
        return R.ok(rydwRyssPo);
    }
    @GetMapping("selectStationListByMineCode")
    @ApiOperation("煤矿分站列表")
    public R<List<RydwRyjzPo>> selectStationListByMineCode(String mineCode) {
        List<RydwRyjzPo> list = rydwService.selectStationListByMineCode(mineCode);
        return R.ok(list);
    }
    @GetMapping("selectLeadPitInfo")
    @ApiOperation("煤矿井下带班领导")
    public R<List<LeadVo>> selectLeadPitInfo(String mineCode) {
        List<LeadVo> leadVos = rydwService.selectLeadPitInfo(mineCode);
        return R.ok(leadVos);
    }
    @GetMapping("getMineCodeListByCompanyCode")
    @ApiOperation("根据公司编码获取当前公司下的煤矿列表")
    public R<List<SysOrganization>> getMineCodeListByCompanyCode(String companyCode) {
        List<SysOrganization> list = aqjkService.getMineCodeListByCompanyCode(companyCode);
        return R.ok(list);
    }
    @GetMapping("getAllCompanyMineCodeList")
    @ApiOperation("获取所有公司列表")
    public R<List<SysOrganization>> getAllCompanyMineCodeList() {
        List<SysOrganization> list = aqjkService.getAllCompanyMineCodeList();
        return R.ok(list);
    }

    @GetMapping("aqjkNowDataList/pageList")
    @ApiOperation("数据查询-安全监控-实时数据")
    public R<ResponsePage<DwdAqjcCdssHVo>> aqjkNowDataList(PageQuery pageQuery, String type, String status, String companyCode, String mineCode) {

        ResponsePage<DwdAqjcCdssHVo> page = dwdService.aqjkNowDataList(pageQuery, type, status,companyCode,mineCode);
        return R.ok(page);
    }

    @GetMapping("aqjkHisDataList/pageList")
    @ApiOperation("数据查询-安全监控-历史数据")
    public R<ResponsePage<DwdAqjcCdssDVo>> aqjkHisDataList(PageQuery pageQuery, String type, String location, String companyCode, String mineCode,
                                                           @ApiParam(value = "开始时间：YYYY-MM-dd HH:mm:ss")@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date startTime,
                                                           @ApiParam(value = "结束时间：YYYY-MM-dd HH:mm:ss")@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date endTime) {

        ResponsePage<DwdAqjcCdssDVo> page = dwdService.aqjkHisDataList(pageQuery, type, location, companyCode, mineCode, startTime, endTime);
        return R.ok(page);
    }
    @GetMapping("aqjkPointDataList/pageList")
    @ApiOperation("数据查询-安全监控-测点信息")
    public R<ResponsePage<DwdAqjcCdssDVo>> aqjkPointDataList(PageQuery pageQuery, String type, String location, String companyCode, String mineCode) {

        ResponsePage<DwdAqjcCdssDVo> page = dwdService.aqjkPointDataList(pageQuery, type, location, companyCode, mineCode);
        return R.ok(page);
    }
    @GetMapping("aqjkAlarmDataList/pageList")
    @ApiOperation("数据查询-安全监控-报警明细")
    public R<ResponsePage<DwdAqjcYcbjDVo>> aqjkAlarmDataList(PageQuery pageQuery, String type, String alarmType, String companyCode, String mineCode,
                                                             @ApiParam(value = "开始时间：YYYY-MM-dd HH:mm:ss")@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date startTime,
                                                             @ApiParam(value = "结束时间：YYYY-MM-dd HH:mm:ss")@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date endTime) {

        ResponsePage<DwdAqjcYcbjDVo> page = dwdService.aqjkAlarmDataList(pageQuery, type, alarmType, companyCode, mineCode,startTime,endTime);
        return R.ok(page);
    }

    @GetMapping("trendByPointCode")
    @ApiOperation("数据查询-测点值时间图")
    public R<PointDateListVo> trendByPointCode(@RequestParam(required = true) String pointCode,
                                               @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME, pattern = "yyyy-MM-dd HH:mm:ss") @ApiParam(example = "yyyy-MM-dd HH:mm:ss  默认结束时间前推6小时") Date startTime,
                                               @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME, pattern = "yyyy-MM-dd HH:mm:ss") @ApiParam(example = "yyyy-MM-dd HH:mm:ss") Date endTime, String mineCode) {
        if (startTime == null) {
            startTime= DateUtils.addHours(new Date(), -6);
        }
        if (endTime == null) {
            endTime=new Date();
        }
        PointDateListVo vo = dwdService.trendByPointCode(pointCode, startTime, endTime, mineCode);
        return R.ok(vo);
    }

    @GetMapping("getMineDwsRydwListByDate")
    @ApiOperation("获取Dws模式的人员数据")
    public R<List<DwsRyDdwRyDFPo>> getMineDwsRydwListByDate(@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME, pattern = "yyyy-MM-dd") @ApiParam(example = "yyyy-MM-dd") Date date){
        List<DwsRyDdwRyDFPo> list = dwsService.getMineDwsRydwListByDate(null,date);
        return R.ok(list);
    }
    @GetMapping("getMineDwsAqjkCdbjListByDate")
    @ApiOperation("获取Dws模式的安全监控测点报警数据")
    public R<List<DwsAqjcYcbjDFPo>> getMineDwsAqjkCdbjListByDate(@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME, pattern = "yyyy-MM-dd") @ApiParam(example = "yyyy-MM-dd") Date date){
        List<DwsAqjcYcbjDFPo> list = dwsService.getMineDwsAqjkCdbjListByDate(null,date);
        return R.ok(list);
    }

    @GetMapping("getCh4AndC0OverrunByMineCode")
    @ApiOperation("获取当前煤矿的甲烷和一氧化碳超限")
    public R<Ch4AndCoOverrunVo> getCh4AndCoOverrunVo(String mineCode){
        Ch4AndCoOverrunVo vo = dwdService.getCh4AndCoOverrunVo(mineCode);
        return R.ok(vo);
    }

    @GetMapping("getPeopleAndleadNumberByMineCode")
    @ApiOperation("获取当前煤矿的井下人数和带班领导数")
    public R<PeopleAndleadNumberVo> getPeopleAndleadNumberByMineCode(String mineCode){
        PeopleAndleadNumberVo vo = rydwService.getPeopleAndleadNumberByMineCode(mineCode);
        return R.ok(vo);
    }
    @GetMapping("getEffectiveProductionTimeByMineCode")
    @ApiOperation("获取有效生产时长列表")
    public R<List<EffectiveProductionTimeByMineCodeDto>> getEffectiveProductionTimeByMineCode(@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME, pattern = "yyyy-MM-dd HH:mm:ss")
                                                                                              @ApiParam(example = "yyyy-MM-dd HH:mm:ss 不传，默认开始时间为今天结束" ) Date endTime,
                                                                                              @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME, pattern = "yyyy-MM-dd HH:mm:ss")
                                                                                              @ApiParam(example = "yyyy-MM-dd HH:mm:ss  不传，默认开始时间为当天开始") Date startTime, String mineCode){
        List<EffectiveProductionTimeByMineCodeDto> vo = dwdService.getEffectiveProductionTimeByMineCode(mineCode, startTime, endTime);
        return R.ok(vo);
    }
    @GetMapping("getEffectiveProductionTimeAllMineCode")
    @ApiOperation("获取有效生产时长列表-所有煤矿")
    public R<List<EffectiveProductionTimeByMineCodeVo>> getEffectiveProductionTimeByMineCode(@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME, pattern = "yyyy-MM-dd HH:mm:ss")
                                                                                              @ApiParam(example = "yyyy-MM-dd HH:mm:ss 不传，默认开始时间为今天结束" ) Date endTime,
                                                                                             @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME, pattern = "yyyy-MM-dd HH:mm:ss")
                                                                                              @ApiParam(example = "yyyy-MM-dd HH:mm:ss  不传，默认开始时间为当天开始") Date startTime){
        List<EffectiveProductionTimeByMineCodeVo> vo = dwdService.getEffectiveProductionTimeAllMineCode(startTime, endTime);
        return R.ok(vo);
    }
    @GetMapping("getEffectiveProductionTimeByMineCodeAndDay")
    @ApiOperation("获取有效生产时长统计")
    public R<CurveVo> getEffectiveProductionTimeByMineCodeAndDay(@ApiParam(value = "天数，不传，默认30天",example = "30",defaultValue = "30")String day, String mineCode){
        CurveVo vo = dwdService.getEffectiveProductionTimeByMineCodeAndDay(mineCode, day);
        return R.ok(vo);
    }

    @GetMapping("jygl/getFineDataCode")
    @ApiOperation("获取jygl编码名称映射表")
    public R<List<JyglDwmcEntityPo>> getFineDataCode(String mineCode){
        List<JyglDwmcEntityPo> vo = jyglDwmcEntityMapper.selectList(new LambdaQueryWrapper<JyglDwmcEntityPo>()
                .eq(StringUtils.isNotEmpty(mineCode),JyglDwmcEntityPo::getOrgId, mineCode));
        return R.ok(vo);
    }


    @GetMapping("jygl/getFineDataClassListBymineName")
    @ApiOperation("获取jygl班次时段")
    public R<List<JyglInsertShiftManage1Po>> getFineDataClassListBymineName(@ApiParam(value = "煤矿名称")String mineName) {
        List<JyglInsertShiftManage1Po> fineData = jyglInsertShiftManage1Mapper.selectList(new LambdaQueryWrapper<JyglInsertShiftManage1Po>()
                .eq(StringUtils.isNotEmpty(mineName),JyglInsertShiftManage1Po::getDw, mineName));
        return R.ok(fineData);
    }

    @GetMapping("jygl/getFineDataBeOnWatchListBymineName")
    @ApiOperation("获取jygl煤矿班次记录")
    public R<List<JyglShiftManagementPo>> getFineDataBeOnWatchListBymineName(@ApiParam(value = "煤矿名称")String mineName,
                                                                             @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME, pattern = "yyyy-MM-dd")
                                                                                @ApiParam(example = "yyyy-MM-dd" ) Date date) {
        List<JyglShiftManagementPo> jyglShiftManagementPos = null;
        if (date == null) {
            jyglShiftManagementPos= jyglShiftManagementMapper.getFineDataBeOnWatchListBymineName(mineName);
        } else {
            jyglShiftManagementPos= jyglShiftManagementMapper.getFineDataBeOnWatchListBymineNameAndDate(mineName,date);
        }
        return R.ok(jyglShiftManagementPos);
    }
    /**
     * 重点设备情况
     */
    @GetMapping("equipmentSwitch")
    @ApiOperation("重点设备情况运行状态")
    public R<List<MineALlSwitchVo>> equipmentSwitch(@ApiParam("公司编码") String companyCode, @ApiParam("煤矿编码") String mineCode){
        List<MineALlSwitchVo> vo=dwdService.equipmentSwitch(companyCode,mineCode);
        return R.ok(vo);
    }
    /**
     * pc-主页-矿井类型
     */
    @GetMapping("mineType")
    @ApiOperation("pc-主页-矿井类型")
    public R<List<MineTypeVo>> mineType(@ApiParam("公司编码") String companyCode,@ApiParam("煤矿编码") String mineCode){
        List<MineTypeVo> vo=dwdService.mineType(companyCode,mineCode);
        return R.ok(vo);
    }

    /**
     * pc-主页-矿井类型
     */
    @GetMapping("safeDays")
    @ApiOperation("pc-主页-安全天数")
    public R<String> safeDays(@ApiParam("公司编码") String companyCode,@ApiParam("煤矿编码") String mineCode){
        String vo=dwdService.safeDays(companyCode,mineCode);
        return R.ok("安全天数",vo);
    }
}
